package com.bianguo.android.recyclerlibrary.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.bianguo.android.recyclerlibrary.holder.RViewHolder;
import com.bianguo.android.recyclerlibrary.listener.ItemListener;
import com.bianguo.android.recyclerlibrary.listener.RViewItem;
import com.bianguo.android.recyclerlibrary.manager.RViewItemManager;

import java.util.ArrayList;

/**
 * RecyclerView多item样式适配器
 * @param <T>
 */
public class RViewAdapter<T> extends RecyclerView.Adapter<RViewHolder> {

    // 数据源
    private ArrayList<T> list;
    // Item的类型管理
    private RViewItemManager<T> itemManager;
    // Item的点击监听
    private ItemListener<T> itemListener;
    private RViewHolder listener;

    // 单样式
    public RViewAdapter(ArrayList<T> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        this.list = list;
        itemManager = new RViewItemManager<>();
    }

    // 多样式
    public RViewAdapter(ArrayList<T> list, RViewItem<T> item) {
        if (list == null) {
            list = new ArrayList<>();
        }
        this.list = list;
        itemManager = new RViewItemManager<>();
        // 将Item的类型加入到管理器
        addItemStyle(item);
    }

    /**
     * 增加一种新的Item样式
     * @param item
     */
    public void addItemStyle(RViewItem<T> item) {
        itemManager.addStyles(item);
    }

    private boolean hasMultiStyle() {
        return itemManager.getItemViewStyleCount() > 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (hasMultiStyle()) {
            return itemManager.getItemViewType(list.get(position), position);
        }
        return super.getItemViewType(position);
    }

    @NonNull
    @Override
    public RViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        RViewItem item = itemManager.getRViewItem(viewType);
        int layoutId = item.getItemLayout();
        RViewHolder viewHolder = RViewHolder.createViewHolder(viewGroup.getContext(), viewGroup, layoutId);
        if (item.openClick()) {
            setListener(viewHolder);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RViewHolder rViewHolder, int position) {
        convert(rViewHolder, list.get(position));
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    public void setItemListener(ItemListener<T> itemListener) {
        this.itemListener = itemListener;
    }

    public void setListener(RViewHolder holder) {
        View convertView = holder.getConvertView();
        convertView.setOnClickListener(view -> {
            if (itemListener != null) {
                int layoutPosition = holder.getLayoutPosition();
                itemListener.onItemClickListener(this, view, list.get(layoutPosition), layoutPosition);
            }
        });

        convertView.setOnLongClickListener(view -> {
            if (itemListener != null) {
                int layoutPosition = holder.getLayoutPosition();
                return itemListener.onItemLongClickListener(this, view, list.get(layoutPosition), layoutPosition);
            }
            return false;
        });
    }

    private void convert(RViewHolder holder, T data) {
        itemManager.convert(holder, data, holder.getAdapterPosition());
    }
}
